<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
use App\Models\Category;
use App\Models\Tag;

class InertiaServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        Inertia::share([
            'auth' => fn () => [
                'user' => auth()->user(),
            ],
            'categories' => fn () => Cache::remember('nav_categories', now()->addMinutes(30), function () {
                return Category::query()
                    ->select('id', 'name', 'slug')
                    ->orderBy('posts_count', 'desc')
                    ->withCount('posts')
                    ->take(5)
                    ->get();
            }),
            'tags' => fn () => Cache::remember('nav_tags', now()->addMinutes(30), function () {
                return Tag::query()
                    ->select('id', 'name', 'slug')
                    ->orderBy('posts_count', 'desc')
                    ->withCount('posts')
                    ->take(10)
                    ->get();
            }),
            'flash' => fn () => [
                'success' => session('success'),
                'error' => session('error'),
            ],
        ]);
    }
}


